package com.common.tools.controller;

import com.common.api.annontation.Authority;
import com.common.api.annontation.ServiceLog;
import com.common.api.enums.OperateEnum;
import com.common.api.model.PageModel;
import com.common.tools.api.dto.DictTypeDto;
import com.common.tools.api.service.DictTypeService;
import com.common.tools.util.BaseBusyServiceUtil;
import com.common.web.annotation.ControllerRequest;
import com.common.web.controller.BaseController;
import com.common.web.dto.request.FuzzyPageDTO;
import com.common.web.dto.response.RestResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * <p>
 * 字典分类表前端控制器
 * </p>
 *
 * @author zhuchao
 */
@ServiceLog
@ControllerRequest("/dictType/")
@Api(tags = "系统工具-字典分类表服务接口")
public class DictTypeController extends BaseController {

    @Autowired
    private DictTypeService dictTypeService;


    /**
     * 查询所有字典分类信息
     *
     * @param fuzzyPageDTO
     * @return
     */
    @ApiOperation(value = "查询所有字典分类信息")
    @RequestMapping(value = "queryList")
    public RestResponse queryList(@RequestBody FuzzyPageDTO fuzzyPageDTO) {
        try {
            PageModel<DictTypeDto> page = dictTypeService.queryList(fuzzyPageDTO);
            return RestResponse.success("字典分类查询成功!").setData(page);
        } catch (Exception e) {
            return RestResponse.failure("字典分类查询失败：" + e.getMessage());
        }
    }


    /**
     * 保存字典分类信息
     *
     * @param
     * @return
     */
    @RequestMapping(value = "save")
    @ApiOperation(value = "保存字典分类信息")
    @Authority(authous = {"dictType/dictTypeEdit"})
    public RestResponse save(@RequestBody DictTypeDto dto) {
        String operation = StringUtils.isBlank(dto.getId()) ? OperateEnum.addDictType.getEnumName() : OperateEnum.updateDictType.getEnumName();
        try {
            executeDictTypeOperation(dto, operation);
            return RestResponse.success(operation + "成功！");
        } catch (Exception e) {
            return RestResponse.failure(operation + "失败：" + e.getMessage());
        }
    }

    private void executeDictTypeOperation(DictTypeDto dto, String operation) {
        if (OperateEnum.addDictType.getEnumName().equals(operation)) {
            logOperateDetails(OperateEnum.addDictType, getUserCode(), dto);
            dictTypeService.add(dto);
        } else if (OperateEnum.updateDictType.getEnumName().equals(operation)) {
            logOperateDetails(OperateEnum.updateDictType, getUserCode(), dto);
            dictTypeService.modify(dto);
        }
    }

    private void logOperateDetails(OperateEnum operateEnum, String userCode, DictTypeDto dto) {
        BaseBusyServiceUtil.addOperateLog(operateEnum, userCode, dto);
    }


    /**
     * 根据主键批量删除字典分类信息
     *
     * @param idList
     * @return
     */
    @RequestMapping(value = "dels")
    @ApiOperation(value = "根据主键批量删除字典分类信息")
    @Authority(authous = {"dictType/dictTypeEdit"})
    public RestResponse dels(@RequestBody List<String> idList) {
        try {
            dictTypeService.dels(idList);
            String logMessage = "删除字典分类" + idList.toString() + "成功！";
            BaseBusyServiceUtil.addOperateLog(OperateEnum.removeDictType, getUserCode(), logMessage);
            return RestResponse.success("字典分类删除成功！");
        } catch (Exception e) {
            return RestResponse.failure("删除字典分类失败：" + e.getMessage());
        }
    }

}
